/**
 * 通用常量定义
 * 包含项目中使用的通用配置常量
 */

// 环境检测
const isDevelopment = import.meta.env.MODE === 'development';
const isProduction = import.meta.env.MODE === 'production';

// API 相关常量
export const API_CONFIG = {
  // 开发环境使用相对路径（通过vite proxy代理）
  // 生产环境使用环境变量或默认值
  BASE_URL: (import.meta.env.VITE_API_BASE_URL || ''),
  
  // 网关地址配置
  GATEWAY_URL: (import.meta.env.VITE_GATEWAY_URL),
  
  // 系统服务URL - 开发环境通过proxy，生产环境使用完整URL
  SYSTEM_SERVICE_URL: (import.meta.env.VITE_SYSTEM_SERVICE_URL),
  TIMEOUT: 30000,
  RETRY_COUNT: 3,
  RETRY_DELAY: 1000,
} as const;

// 网关服务路径配置
export const GATEWAY_CONFIG = {
  // 开发环境：通过vite proxy代理，直接使用相对路径
  // 生产环境：使用完整的网关地址
  BASE_URL: (import.meta.env.VITE_GATEWAY_URL),
  // 服务路径
  SYSTEM_SERVICE: '/system',
  AUTH_SERVICE: '/auth', 
  GATEWAY_SERVICE: '/gateway',
} as const;

// API URL构建器工具函数
export const apiUtils = {
  /**
   * 构建完整的API URL
   * @param servicePath 服务路径 (如: '/system', '/auth')
   * @param apiPath API路径 (如: '/api/v3/platform/auth/login')
   * @returns 完整的URL
   */
  buildApiUrl: (servicePath: string, apiPath: string): string => {
    if (isDevelopment) {
      // 开发环境：直接使用API路径，通过vite proxy代理
      return apiPath;
    } else {
      // 生产环境：构建完整URL
      return `${GATEWAY_CONFIG.BASE_URL}${servicePath}${apiPath}`;
    }
  },

  /**
   * 构建平台API URL (通过system服务)
   * @param apiPath API路径
   * @returns 完整的平台API URL
   */
  buildPlatformApiUrl: (apiPath: string): string => {
    if (isDevelopment) {
      // 开发环境：直接返回API路径，通过proxy代理到网关
      return apiPath;
    } else {
      // 生产环境：构建完整URL
      return apiUtils.buildApiUrl(GATEWAY_CONFIG.SYSTEM_SERVICE, apiPath);
    }
  },

  /**
   * 构建系统API URL (通过system服务)
   * @param apiPath API路径
   * @returns 完整的系统API URL
   */
  buildSystemApiUrl: (apiPath: string): string => {
    return apiUtils.buildPlatformApiUrl(apiPath);
  },

  /**
   * 构建认证API URL (通过auth服务)
   * @param apiPath API路径
   * @returns 完整的认证API URL
   */
  buildAuthApiUrl: (apiPath: string): string => {
    if (isDevelopment) {
      // 开发环境：使用相对路径
      return apiPath;
    } else {
      // 生产环境：构建完整URL
      return apiUtils.buildApiUrl(GATEWAY_CONFIG.AUTH_SERVICE, apiPath);
    }
  },

  /**
   * 获取当前环境信息
   */
  getEnvironment: () => ({
    isDevelopment,
    isProduction,
    mode: import.meta.env.MODE,
    baseUrl: API_CONFIG.BASE_URL,
    gatewayUrl: GATEWAY_CONFIG.BASE_URL,
  }),
} as const;

// API 响应码
export const API_CODE = {
  SUCCESS: 0,
  ERROR: -1,
  UNAUTHORIZED: 401,
  FORBIDDEN: 403,
  NOT_FOUND: 404,
  VALIDATION_ERROR: 422,
} as const;

// 内容类型
export const CONTENT_TYPE = {
  JSON: 'application/json',
  FORM_DATA: 'multipart/form-data',
  FORM_URLENCODED: 'application/x-www-form-urlencoded',
  TEXT: 'text/plain',
  HTML: 'text/html',
} as const;

// 正则表达式
export const REGEX = {
  EMAIL: /^[^\s@]+@[^\s@]+\.[^\s@]+$/,
  PHONE: /^1[3-9]\d{9}$/,
  PASSWORD: /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[a-zA-Z\d@$!%*?&]{8,}$/,
  USERNAME: /^[a-zA-Z0-9_-]{3,20}$/,
  URL: /^https?:\/\/.+/,
  CODE: /^[A-Z0-9_]+$/,
} as const;

// HTTP 方法
export const HTTP_METHODS = {
  GET: 'GET',
  POST: 'POST',
  PUT: 'PUT',
  DELETE: 'DELETE',
  PATCH: 'PATCH',
} as const;

// HTTP状态码
export const HTTP_STATUS = {
  OK: 200,
  CREATED: 201,
  NO_CONTENT: 204,
  BAD_REQUEST: 400,
  UNAUTHORIZED: 401,
  FORBIDDEN: 403,
  NOT_FOUND: 404,
  INTERNAL_SERVER_ERROR: 500,
} as const;

// 分页配置
export const PAGINATION = {
  DEFAULT_PAGE_SIZE: 10,
  PAGE_SIZE_OPTIONS: ['10', '20', '50', '100'],
  SHOW_SIZE_CHANGER: true,
  SHOW_QUICK_JUMPER: true,
  SHOW_TOTAL: (total: number, range: [number, number]) => 
    `第 ${range[0]}-${range[1]} 条，共 ${total} 条`,
} as const;

// 日期格式
export const DATE_FORMAT = {
  DATE: 'YYYY-MM-DD',
  DATETIME: 'YYYY-MM-DD HH:mm:ss',
  TIME: 'HH:mm:ss',
  MONTH: 'YYYY-MM',
  YEAR: 'YYYY',
} as const;

// 状态定义
export const STATUS = {
  ACTIVE: 'active',
  INACTIVE: 'inactive',
  PENDING: 'pending',
  DISABLED: 'disabled',
  DELETED: 'deleted',
} as const;

// 状态颜色映射
export const STATUS_COLOR_MAP = {
  [STATUS.ACTIVE]: 'success',
  [STATUS.INACTIVE]: 'default',
  [STATUS.PENDING]: 'processing',
  [STATUS.DISABLED]: 'warning',
  [STATUS.DELETED]: 'error',
} as const;

// 状态文本映射
export const STATUS_TEXT_MAP = {
  [STATUS.ACTIVE]: '正常',
  [STATUS.INACTIVE]: '未激活',
  [STATUS.PENDING]: '待审核',
  [STATUS.DISABLED]: '已禁用',
  [STATUS.DELETED]: '已删除',
} as const;

// 操作类型
export const ACTION_TYPE = {
  CREATE: 'create',
  UPDATE: 'update',
  DELETE: 'delete',
  VIEW: 'view',
  EXPORT: 'export',
  IMPORT: 'import',
  BATCH: 'batch',
} as const;

// 排序方向
export const SORT_ORDER = {
  ASC: 'asc',
  DESC: 'desc',
} as const;

// 本地存储键名
export const STORAGE_KEYS = {
  TOKEN: 'token',
  USER: 'user',
  TENANT: 'tenant',
  PLATFORM_TOKEN: 'platform_token',
  PLATFORM_USER: 'platform_user',
  THEME: 'theme',
  LANGUAGE: 'language',
  SIDEBAR_COLLAPSED: 'sidebar_collapsed',
} as const;

// 用户状态
export const USER_STATUS = {
  ACTIVE: 'active',
  INACTIVE: 'inactive',
  LOCKED: 'locked',
} as const;

// 权限类型
export const PERMISSION_TYPES = {
  MENU: 'menu',
  BUTTON: 'button',
  API: 'api',
} as const;